package com.airlines.crewassignment.service.linkline.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.entity.dict.Select2;
import com.airlines.base.service.base.impl.BaseServiceImpl;
import com.airlines.common.DateTimeRangeUtil;
import com.airlines.common.TimeUtil;
import com.airlines.common.WeekUtil;
import com.airlines.crewassignment.dao.linkline.TaskLoopFlightMapper;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.entity.linkline.TaskLoop;
import com.airlines.crewassignment.entity.linkline.TaskLoopFlight;
import com.airlines.crewassignment.service.dict.Select2Service;
import com.airlines.crewassignment.service.linkline.TaskLoopFlightService;

/**
 * @author xxxx
 * @date 2018年6月9日 
 * @description
 */
@Service
public class TaskLoopFlightServiceImpl extends BaseServiceImpl<TaskLoopFlight, Integer> implements TaskLoopFlightService{
	@Autowired
	private TaskLoopFlightMapper taskLoopFlightMapper;
	@Autowired
	@Override
	public void setBaseMapper() {
		super.setBaseMapper(taskLoopFlightMapper);
	}
	@Autowired
	private Select2Service select2Service;
	@Override
	public List<TaskLoopFlight> listTaskLoopFlightByLoopId(int loopId) {
		TaskLoopFlight query = new TaskLoopFlight();
		query.setLoopId(loopId);
		List<TaskLoopFlight> list = this.listData(query);
		
		List<Select2> airportList = select2Service.getAirportCityCnName();
		select2Service.convert(list, "departureAirport", "departureAirportName", airportList);
		select2Service.convert(list, "arrivalAirport", "arrivalAirportName", airportList);
		
		return list;
	}
	@Override
	public int saveOrUpdate(TaskLoopFlight taskLoopFlight) {
		if(taskLoopFlight.getId() == null){
			return this.save(taskLoopFlight);
		}else{
			return this.updateByPrimaryKey(taskLoopFlight);
		}
	}
	@Override
	public boolean matching(TaskLoop taskLoop,TaskLoopFlight taskLoopFlight, FlightInfo flightInfo) {
		boolean flag = true;
		
		flag = flag && StringUtils.equals(taskLoopFlight.getFlightNo(), flightInfo.getFlightNo());
		flag = flag && StringUtils.equals(taskLoopFlight.getDepartureAirport(), flightInfo.getDepartureAirport());
		flag = flag && StringUtils.equals(taskLoopFlight.getArrivalAirport(), flightInfo.getArrivalAirport());
		
		int flightWeek  = WeekUtil.getWeek(flightInfo.getFlightDate());
		
		flag = flag && WeekUtil.weekCheck(taskLoopFlight.getWeeks(), flightWeek);
		
		//判断有效期是否匹配
		DateTimeRange taskDateTimeRange = new DateTimeRange(taskLoop.getStartDate(),TimeUtil.getDateDayEndTime(taskLoop.getEndDate()));
		DateTimeRange flightDateRange = new DateTimeRange(flightInfo.getFlightDate(),TimeUtil.getDateDayEndTime(flightInfo.getFlightDate()));
		
		flag = flag && DateTimeRangeUtil.isIntersection(taskDateTimeRange, flightDateRange);
		
		return flag;
	}

}
